/* eslint-disable no-underscore-dangle */
import './polyfill';
import dva from 'dva';
import fetch from 'dva/fetch';
import { notification } from 'antd';

import createHistory from 'history/createHashHistory';
// user BrowserHistory
// import createHistory from 'history/createBrowserHistory';
import createLoading from 'dva-loading';
import 'moment/locale/zh-cn';
import './rollbar';

import './index.less';

export default fetch('/api/getMenu', { method: 'POST' })
  .then(res => {
    return res.json();
  })
  .then(json => {
    // 1. Initialize
    const app = dva({
      history: createHistory(),
      initialState: {
        global: {
          menuData: json,
        },
      },
    });
    // 2. Plugins
    app.use(createLoading());

    // 3. Register global model
    app.model(require('./models/global').default);

    // 4. Router
    app.router(require('./router').default);

    // 5. Start
    app.start('#root');
    return app._store;
  })
  .catch(() => {
    notification.error({
      message: '初始化系统失败！',
    });
    return null;
  });
